'use strict'

module.exports = (app) => {
  const { STRING, INTEGER, DATE } = app.Sequelize
  // 配置（重要：一定要配置详细，一定要！！！）
  const Role = app.model.define('role', {
    id: {
      type: INTEGER(20).UNSIGNED,
      primaryKey: true,
      autoIncrement: true
    },
    role_name: {
      type: STRING(30),
      comment: '角色名称'
    },
    created_time: {
      type: DATE,
      get() {
        return app.formatTime(this.getDataValue('created_time'))
      }
    }
  })
  // 关联关系
  Role.associate = (models) => {
    // 多对多 管理员
    Role.belongsToMany(app.model.AdminUser, {
      through: app.model.UserRole,
      foreignKey: 'rid',
      otherKey: 'uid'
    })
    // 多对多 权限
    Role.belongsToMany(app.model.Permissions, {
      through: app.model.RolePermissions,
      foreignKey: 'rid',
      otherKey: 'pid'
    })
  }

  return Role
}
